package com.jjwxc.jwjskandriod.my.fragment

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.GridLayoutManager
import com.jjwxc.jwjskandriod.R
import com.jjwxc.jwjskandriod.databinding.FragmentPortraitBinding
import com.jjwxc.jwjskandriod.framework.base.FFApplication
import com.jjwxc.jwjskandriod.model.VIPBgResponse
import com.jjwxc.jwjskandriod.model.request.UserProfileRequest
import com.jjwxc.jwjskandriod.my.activity.SelectAvatarActivity
import com.jjwxc.jwjskandriod.my.adapter.PortraitAdapter
import com.jjwxc.jwjskandriod.my.data.UserInfoBg.USER_ICON
import com.jjwxc.jwjskandriod.my.data.UserInfoBg.USER_ICON_RID
import com.jjwxc.jwjskandriod.widget.LinearLayouDecoration

class PortraitFragment : Fragment() {
    private var mActivity: SelectAvatarActivity? = null
    private var dataBinding: FragmentPortraitBinding? = null
    private val mList: ArrayList<VIPBgResponse> = arrayListOf()
    private val adapter: PortraitAdapter by lazy {
        PortraitAdapter(0)
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        mActivity = activity as SelectAvatarActivity
        dataBinding = FragmentPortraitBinding.inflate(inflater, container, false)

        initView()
        initData()
        return dataBinding?.root!!
    }

    @SuppressLint("NotifyDataSetChanged")
    private fun initView() {
        // RecyclerView配置信息
        dataBinding?.rvPortrait?.apply {
            isNestedScrollingEnabled = false
            isFocusable = false
            // 網格佈局，每行數量
            layoutManager = GridLayoutManager(activity, 3)
            // 間距
            addItemDecoration(
                LinearLayouDecoration(
                    activity,
                    LinearLayouDecoration.HORIZONTAL,
                    R.dimen.border8,
                    R.color.transparent
                )
            )
            // 動畫時長
            itemAnimator?.changeDuration = 0
            adapter = this@PortraitFragment.adapter
        }

        dataBinding?.tvUse?.setOnClickListener {
            if (!mActivity?.userIsVip!!) {
//                FFApplication.showToast("當前還不是VIP，暫時不能使用VIP功能")
                mActivity?.toastVipInfo(it)
                return@setOnClickListener
            }
            for (i in mList.indices) {
                if (mList[i].isSelect) {
                    updateUserProfile(i)
                }
            }
            FFApplication.showToast("头像已修改")
            adapter.notifyDataSetChanged()
        }
    }

    private fun updateUserProfile(i: Int) {
        val request = UserProfileRequest(
            profileImgUrl = USER_ICON[i]
        )

        mActivity?.updateUserProfile(request)
    }

    /**
     * 初始化數據
     */
    private fun initData() {
        afterCreate()
        // 獲取Vip信息
        mActivity?.getVipDistance()
    }

    /**
     * 頭像數據初始化
     */
    private fun afterCreate() {
        for (i in USER_ICON.indices) {
            val response = VIPBgResponse().apply {
                id = USER_ICON[i]
                url = USER_ICON_RID[i]

                isSelect = USER_ICON[i] == mActivity?.profileImgUrl
            }

            mList.add(response)
        }

        adapter.setData(mList)
    }
}